package com.rajaev.dtu.tractor.interfaces;

import com.github.pagehelper.PageInfo;

import com.rajaev.dtu.tractor.app.service.BatteryService;
import com.rajaev.dtu.tractor.app.service.CustomerService;
import com.rajaev.dtu.tractor.domain.vo.BatteryMonitorVo;
import com.rajaev.dtu.tractor.domain.vo.CustomerVo;
import com.rajaev.dtu.tractor.domain.entity.UserInfo;
import com.rajaev.dtu.tractor.domain.service.CustomerServiceDomain;
import com.rajaev.dtu.tractor.infrastructure.base.errors.BusinessException;
import com.rajaev.dtu.tractor.infrastructure.helper.UserSecurityUtil;
import com.rajaev.dtu.tractor.interfaces.dto.CustomerWithProjectTeamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class CustomerController  {

    @Autowired
    CustomerService customerService;

    @Autowired
    BatteryService batteryService;

    @Autowired
    UserSecurityUtil userSecurityUtil;



    @PostMapping("/customer/add")
    public void save(CustomerVo customerVo, HttpServletRequest request) throws BusinessException {
        UserInfo userInfo = userSecurityUtil.getLoginUser(request).getUserInfo();
        customerService.save(customerVo,userInfo.getId());
    }


    @PostMapping("/customer/edit")
    public void edit(CustomerVo customerVo) throws BusinessException {
        customerService.updateCustomer(customerVo);
    }

    @PostMapping("/customer/delete")
    public void delete(Integer customerId) throws BusinessException {
        customerService.deleteCustomer(customerId);
    }

    @GetMapping("/customer/getList")
    public PageInfo<CustomerVo> pageQueryCustomer(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                  @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize) {


        return customerService.pageQueryCustomer(pageSize, pageNum);
    }


    @GetMapping("customer/query")
    public List<CustomerVo> queryByName(String customerName){
        return customerService.selectCustomerVosByName(customerName);
    }


    @GetMapping("/customer/listUserCustom")
    public List<CustomerWithProjectTeamVo> listCustomers(HttpServletRequest request) {
        UserInfo userInfo = userSecurityUtil.getLoginUser(request).getUserInfo();
        return customerService.selectCustomerVosByUserId(userInfo.getId());
    }


    @GetMapping("/customer/selectBatteries")
    public List<BatteryMonitorVo> selectBatteries(Integer customerId, HttpServletRequest request){
        return batteryService.selectByCustomerId(customerId);
    }

    @GetMapping("/customer/selectBatteriesAll")
    public List<BatteryMonitorVo> selectBatteries(HttpServletRequest request){
        UserInfo userInfo = userSecurityUtil.getLoginUser(request).getUserInfo();

        return batteryService.selectBatteriesWithCustomer(userInfo.getId());
    }

}